package com.choudou5.solr.util.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import com.choudou5.solr.util.LogDeBugUtil;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @name 数据源工厂
 * @author choudou5
 * @date 2018-7-13
 */
public class DataSourceFactory {

    private static DruidDataSource dataSource = null;

    private DataSourceFactory(){}

    public static DataSource getDataSource(){
        return dataSource;
    }

    public static void initDataSource(DataSourceConfig config) throws SQLException {
        LogDeBugUtil.debug("初始化数据源-------开始");
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(config.getDriverClass());
        dataSource.setUrl(config.getJdbcUrl());
        dataSource.setUsername(config.getUsername());
        dataSource.setPassword(config.getPassword());
        dataSource.setInitialSize(config.getInitialSize());
        dataSource.setMaxActive(config.getMaxActive());
        dataSource.setMinIdle(config.getMinIdle());
        dataSource.setMaxWait(config.getMaxWait());
        dataSource.setRemoveAbandoned(config.isRemoveAbandoned());
        dataSource.setRemoveAbandonedTimeout(config.getRemoveAbandonedTimeoutMillis());
        dataSource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
        dataSource.setValidationQuery(config.getValidationQuery());
        dataSource.setTestWhileIdle(config.isTestWhileIdle());
        dataSource.setTestOnBorrow(config.isTestOnBorrow());
        dataSource.setTestOnReturn(config.isTestOnReturn());
        dataSource.setPoolPreparedStatements(config.isPoolPreparedStatements());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(config.getMaxPoolPreparedStatementPerConnectionSize());
        dataSource.setFilters(config.getFilters());
        dataSource.init();
    }

    public static void closeDataSource(){
        LogDeBugUtil.debug("关闭 DataSource");
        dataSource.close();
    }

//    private static void initDataSource(DataSourceConfig config) throws SQLException {
//        dataSource = new DruidDataSource();
//        dataSource.setDriverClassName(ConfigUtil.getStr("jdbc.driver"));
//        dataSource.setUrl(ConfigUtil.getStr("jdbc.url"));
//        dataSource.setUsername(ConfigUtil.getStr("jdbc.username"));
//        dataSource.setPassword(ConfigUtil.getStr("jdbc.password"));
//        dataSource.setInitialSize(ConfigUtil.getInt("jdbc.initialSize"));
//        dataSource.setMaxActive(ConfigUtil.getInt("jdbc.maxActive"));
//        dataSource.setMinIdle(ConfigUtil.getInt("jdbc.minIdle"));
//        dataSource.setMaxWait(ConfigUtil.getInt("jdbc.maxWait"));
//        dataSource.setRemoveAbandoned(ConfigUtil.getBool("jdbc.removeAbandoned"));
//        dataSource.setRemoveAbandonedTimeout(ConfigUtil.getInt("jdbc.removeAbandonedTimeout"));
//        dataSource.setTimeBetweenEvictionRunsMillis(ConfigUtil.getLong("jdbc.timeBetweenEvictionRunsMillis"));
//        dataSource.setMinEvictableIdleTimeMillis(ConfigUtil.getLong("jdbc.minEvictableIdleTimeMillis"));
//        dataSource.setValidationQuery(ConfigUtil.getStr("jdbc.validationQuery"));
//        dataSource.setTestWhileIdle(ConfigUtil.getBool("jdbc.testWhileIdle"));
//        dataSource.setTestOnBorrow(ConfigUtil.getBool("jdbc.testOnBorrow"));
//        dataSource.setTestOnReturn(ConfigUtil.getBool("jdbc.testOnReturn"));
//        dataSource.setPoolPreparedStatements(ConfigUtil.getBool("jdbc.poolPreparedStatements"));
//        dataSource.setMaxPoolPreparedStatementPerConnectionSize(ConfigUtil.getInt("jdbc.maxPoolPreparedStatementPerConnectionSize"));
//        dataSource.setFilters(ConfigUtil.getStr("jdbc.filters"));
//        dataSource.init();
//    }
}
